const { mocks } = require("./index.js");
const chalk = require("chalk");

module.exports = (middlewares, devServer) => {
	if (!devServer) {
		throw new Error("dev-server setupMiddlewares is not defined");
	}

	mocks.forEach((item) => {
		const { url, type = "get", response } = item;
		console.log(chalk.green(`> Mock Server: ${type} ${url}`));
		devServer.app[type](url, (_, res) => {
			const data = response instanceof Function ? response(_) : response;
			res.json(data);
		});
	});
	return middlewares;
};
